<template>
  <div>
    <el-row>
      <el-col :span="3">
        <el-input v-model="employee_name" placeholder="请输入员工名称"></el-input>
      </el-col> 
      <el-col :span="20">
        <el-button @click="clolr_click" class="add_btn" icon="el-icon-search">
          搜索</el-button
        >
        <el-button @click="clolr_clicks" icon="el-icon-refresh" class="add_btn">
          重置</el-button
        >
        <el-button @click="search(2)" class="add_btn" icon="el-icon-search">
          导出</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="dataList" stripe style="width: 100%" border>
      <el-table-column label="员工姓名">
        <template slot-scope="scope">
          <span v-if="scope.row.employee">
            {{ scope.row.employee.employee_name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="order_count" label="工单完成数"> </el-table-column>
      <el-table-column prop="base_pay" label="员工工资">
        <template slot-scope="scope">
          <span v-if="scope.row.employee">
            {{ scope.row.employee.base_pay }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="total_sales" label="工单总金额"> </el-table-column>
      <el-table-column prop="created_at" label="合计">
        <template slot-scope="scope">
          <span v-if="scope.row.employee && scope.row.total_sales">
            {{
              Number(scope.row.employee.base_pay) +
              Number(scope.row.total_sales)
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click="set_product(scope.row.employee.employee_name,scope.row.employee.user_id)"
            type="text"
            icon="el-icon-edit"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="tits +'工资详情'"
      :visible.sync="isjilu"
      width="55%"
      @close="close_dialog"
    >
      <div>
        <!-- <el-row>
        <el-col :span="3"> 
          <el-input v-model="color" placeholder="请输入员工名称"></el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="clolr_click"  class="add_btn"  icon="el-icon-search"> 搜索</el-button>
        </el-col>
      </el-row> -->
        <el-table :data="dataList_2" stripe style="width: 100%" border>
          <el-table-column label="客户">
            <template slot-scope="scope">
              {{ scope.row.order.customer.customer_name }}
            </template>
          </el-table-column>
          <el-table-column  label="订单号">
            <template slot-scope="scope">
              {{ scope.row.order.order_number }}
            </template>
          </el-table-column>
          <el-table-column prop="order_count" label="产品">
            <template slot-scope="scope">
              {{ scope.row.order.product.product_name }}
            </template>
          </el-table-column>
          <el-table-column label="颜色">
            <template slot-scope="scope">
              {{ scope.row.order.color.color }}
            </template>
          </el-table-column>
          <el-table-column label="型号">
            <template slot-scope="scope">
              {{ scope.row.order.model_number.model_number }}
            </template>
          </el-table-column>
          <el-table-column label="规格">
            <template slot-scope="scope">
              {{ scope.row.order.specification.specification }}
            </template>
          </el-table-column>
          <el-table-column label="金额">
            <template slot-scope="scope">
              {{ scope.row.price }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                @click="set_product_2(scope.row)"
                type="text"
                icon="el-icon-edit"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange_2"
            @current-change="handleCurrentChange_2"
            background
            @prev-click="prev_2"
            @next-click="next_2"
            :current-page.sync="pagination_2.page"
            :page-size="pagination_2.per_page"
            layout="total, prev, pager, next"
            :total="total_2"
          >
          </el-pagination>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isjilu = false">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="30%"
      :close-on-click-modal="false"
      @close="close_dialog_2"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="金额" prop="color">
          <el-input-number :precision="0"
            v-model="ruleForm.price"
            placeholder="请输入金额"
          ></el-input-number>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd_2('ruleForm')"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getSalaryList,
  createColor,
  getSalaryInfo,
  changeSalary,
} from "@/api/product/index";
export default {
  data() {
    return {
      tits:"",
      isjilu:false,
      employee_name: "",
      title: "",
      isProduct: false,
      dataList: [],
      dataList_2: [],
      ruleForm: {
        color: "",
        id: "",
      },
      rules: {
        color: [
          {
            required: true,
            message: "请输入颜色或以英文, 号隔开",
            trigger: "blur",
          },
        ],
      },
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      ruleForm: {
        order_child_id: "",
        price: "",
      },
      rules: {
        price: [
          {
            required: true,
            message: "请输入金额",
            trigger: "blur",
          },
        ],
      },
      pagination_2: {
        per_page: 10,
        page: 1,
      },
      total_2: 0,
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    search(type) {
      getSalaryList(this.ser).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
          if (type == 2) {
            if (res.data.data.length > 0) {
              this.$message.success("正在导出订单列表");
              this.exports_w();
            }
          }
        }
      });
    },
    exports_w() {
      let data = {
        ...this.ser,
        export: 1,
      };
      getSalaryList(data).then((res) => {
        //创建一个隐藏的a连接，
        const link = document.createElement("a");
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        link.style.display = "none";
        //设置连接
        link.href = URL.createObjectURL(blob);
        link.download = "员工工资列表";
        document.body.appendChild(link);
        //模拟点击事件
        link.click();
      });
    },
    close_dialog() {
      this.ruleForm = {
        color: "",
        id: "",
      };
    },
    getlist() {
      getSalaryList({ ...this.employee_name, ...this.pagination }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    set_product(name,id) {
      console.log(id);
      this.isjilu=true
      this.tits=name
      console.log(name)
      this.getlist_2(id)
    },
    add_color() {
      this.title = "新增颜色";
      this.isProduct = true;
      this.$nextTick(function () {
        this.$refs.ruleForm.resetFields();
      });
    },
    submitadd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          createColor(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleSizeChange(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
    clolr_click() {
      getSalaryList({ color: this.color }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    clolr_clicks() {
      this.color = "";
      getSalaryList(this.pagination).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    //
    close_dialog_2() {
      this.ruleForm = {
        order_child_id: "",
        price: "",
      };
    },
    getlist_2(id) {
      getSalaryInfo({ employee_user_id: id }).then((res) => {
        if (res.code == 200) {
          this.dataList_2 = res.data.data;
          this.total_2 = res.data.total;
          console.log(this.dataList_2)
        }
      });
    },
    set_product_2(row) {
      this.ruleForm = row;
      this.ruleForm.order_child_id = row.id;
      this.title = "工资修改";
   
      this.isProduct = true;
    },
    submitadd_2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          changeSalary(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist(this.id);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleSizeChange_2(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange_2(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist(this.id);
    },
    prev_2() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist(this.id);
    },
    next_2() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist(this.id);
    },
  },
};
</script>

<style></style>
